package cz.muni.fi.pa165.languageschool;

import cz.muni.fi.pa165.languageschool.utils.Authority;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Entity representing Persons super type
 *
 * @author Michal Fučík (395624) michal.fuca.fucik(at)gmail.com
 */
@Entity
@XmlRootElement
public abstract class Person implements Comparable<Person>, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Person_id")
    private Long Id;
    private String firstName;
    private String lastName;
    @Column(name = "USERNAME", length = 30)
    private String username;
    @Column(name = "PASSWORD", length = 30)
    private String password;
    @Column(name = "ENABLED")
    private boolean enabled;
    @Column(name = "AUTHORITY")
    @Basic
    @Enumerated(EnumType.STRING)
    private Authority authority;

    // -- Methods --------------------------------------------------------------
    /**
     * Id propety setter
     *
     * @param Id
     */
    public void setId(Long Id) {
        this.Id = Id;
    }

    /**
     * Id property getter
     *
     * @return
     */
    public Long getId() {
        return Id;
    }

    /**
     * FirstName property getter
     *
     * @return firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * FirstName property getter
     *
     * @param firstName
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * LastName property getter
     *
     * @return lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * LastName property setter
     *
     * @param lastName
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * UserName property getter
     *
     * @return username
     */
    public String getUsername() {
        return username;
    }

    /**
     * UserName property setter
     *
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Password property getter
     *
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Password property setter
     *
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Is enabled predicate getter
     *
     * @return
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Enabled predicate setter
     *
     * @param enabled
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * Authority property getter (represents role within application)
     *
     * @return
     */
    public Authority getAuthority() {
        return authority;
    }

    /**
     * Authority property setter
     *
     * @param authority
     */
    public void setAuthority(Authority authority) {
        this.authority = authority;
    }

    @Override
    public abstract int hashCode();

    @Override
    public abstract boolean equals(Object obj);

    @Override
    public int compareTo(Person obj) {
        int last = obj.getLastName().compareTo(this.getLastName());
        int first = 0;
        if (last == 0) {
            first = obj.getFirstName().compareTo(this.getFirstName());
            if (first == 0) {
                return obj.getId().compareTo(getId());
            } else {
                return first;
            }
        } else {
            return last;
        }
    }

    @Override
    public String toString() {
        return "[Person {id=" + getId() + ", lastName=" + getLastName() + ", firstName=" + getFirstName() + ", role=" + getAuthority() + "}]";
    }
}
